package com.yonyou.pmclouds.inspectitem.service.rmiimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.procedure.entity.ProcedureRefVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;

public class DealRefFiledsUtil {
	/**
	 * 根据REF_FILEDS设置参照信息
	 * @throws BusinessException 
	 */
    public static InspectItemVO[] setRefFiledINfo(ProcedureQuery processQuery,InspectItemVO[] vos) throws BusinessException {
	    if(ArrayUtils.isEmpty(vos)) {
			return null;
		}
		// 参照字段-参照主键集合
		Map<String,List<String>> filed_pksMap = new HashMap<String, List<String>>();
		// 获取filed_pksMap
		for(SuperVO vo : vos) {
			for(String filed : InspectItemVO.REF_FILEDS) {
				List<String> refFiledPks = filed_pksMap.get(filed);
				if(refFiledPks == null || refFiledPks.size() == 0) {
					refFiledPks = new ArrayList<String>();
				}
				refFiledPks.add((String) BeanHelper.getProperty(vo, filed));
				filed_pksMap.put(filed, refFiledPks);
			}
		}
		// 工序vos
		 Map<String, ProcedureRefVO> proceVOMap = null; 
		//TODO:检查项vos
		//TODO: 问题原因vos
		for (String filed : InspectItemVO.REF_FILEDS) {
			List<String> pks = filed_pksMap.get(filed);
			String[] str_pks = pks.toArray(new String[pks.size()]);
			if(ArrayUtils.isEmpty(str_pks)) {
			    continue;
			}
			//TODO:查询检查项
			if(InspectItemVO.PK_ITEM.equals(filed)) {
			  
			 //TODO: 查询问题原因
			} else if(InspectItemVO.PK_PROBLEMREASON.equals(filed)) {
		     
			//查询工序
			} else if(SiteInspectionVO.PK_PROCESS.equals(filed)) {
			    proceVOMap = processQuery.queryRefs(str_pks);
			}
		}
		
		// 当原有的主键没有对应的对象，原有的档案可能被删除掉了，此时引用该档案的单据显示问题【待处理】
		for(InspectItemVO vo :vos) {
		    // 工序字段处理
           if(proceVOMap != null && proceVOMap.keySet().size() != 0) {
               ProcedureRefVO procedureRefVO = proceVOMap.get(vo.getPk_process());
               vo.setProcess_name(procedureRefVO.getSname());
           }
            
        }
        return vos;
	}
}
